from fastapi import Request
from fastapi.responses import JSONResponse
from utils.exceptions import BusinessException
from utils.response import ResponseModel

async def exception_handler(request: Request, exc: Exception) -> JSONResponse:
    # 处理自定义业务异常
    if isinstance(exc, BusinessException):
        return JSONResponse(
            status_code=200,
            content=exc.detail if hasattr(exc, 'detail') else str(exc)
        )
    
    # 处理其他未知异常
    return JSONResponse(
        status_code=200,
        content=ResponseModel.error(
            code=500,
            message=str(exc) if str(exc) else "服务器内部错误"
        )
    ) 